database connection.ipynb
No Headings
The table of contents shows headings in notebooks and supported files.
- File
- Edit
- View
- Run
- Kernel
- Settings
- Help
Kernel status: Idle
[7]:
Selection deleted
import sqlite3
[9]:
#connect to database
conn = sqlite3.connect("company.db")
cursor = conn.cursor()
[11]:
# create Employees table
cursor.execute("""
CREATE TABLE IF NOT EXISTS Employees (
employee_id INT PRIMARY KEY,
name TEXT,
department TEXT,
joining_date DATE
)
""")
[11]:
<sqlite3.Cursor at 0x2a2d2fa9040>
[13]:
conn.commit()
[15]:
#insert employee data
cursor.execute("INSERT INTO Employees VALUES (1, 'Alice', 'HR', '2023-01-15')")
cursor.execute("INSERT INTO Employees VALUES (2, 'Bob', 'IT', '2023-02-01')")
conn.commit()
[17]:
#Fetch Employee Data
cursor.execute("SELECT * FROM Employees")
print(cursor.fetchall())
[(1, 'Alice', 'HR', '2023-01-15'), (2, 'Bob', 'IT', '2023-02-01')]
[31]:
# create Attendance table table
cursor.execute("""
CREATE TABLE IF NOT EXISTS Attendance (
attendance_id INT PRIMARY KEY,
employee_id INT
Date DATE,
Status VARCHAR(10),-- present/Absent/Late,
FOREIGN KEY (employee_id) REFERENCES Attendance(employee_id)
)
""")
[31]:
<sqlite3.Cursor at 0x2a2d2fa9040>
[33]:
conn.commit()
[(1, '2023-01-15', 'present'), (2, '2023-02-01', 'Late')]
[49]:
<sqlite3.Cursor at 0x2a2d2fa9040>
[(1, 2052, '2023-01-15', 5), (2, 2062, '2023-02-01', 10)]
Empty DataFrame Columns: [name, avg_score] Index: []
[ ]:
Notebook cell shifted down successfully
Common Tools
No metadata.
Advanced Tools
No metadata.
Anaconda Assistant
AI-powered coding, insights and debugging in your notebooks.
To enable the following extensions, create an account or sign in.
- Anaconda Assistant4.1.0
- Coming soon!
- Data Catalogs
- Panel Deployments
- Sharing
Already have an account? Sign In
For more information, read our Anaconda Assistant documentation.
![Python [conda env:base] *](./database connection_files/logo-64x64.png)